<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>比赛管理</title>
   <!-- bootstrap -->
   <script src="vendor/jquery/jquery.min.js"></script>
   <link href="vendor/bootstrap/css/bootstrap.min.css"  rel="stylesheet">
   <script src="vendor/bootstrap/js/bootstrap.min.js"></script>

   <!-- 主要css-->
   <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
 
   <!-- vue和axios -->
   <script src="js/vue.min.js"></script>
   <script src="js/axios.min.js"></script>
   
  <!-- js -->
   <script src="js/header.js"></script>

   <script src="vendor/moment/2.22.2/moment.js"></script> 
</head>
<body>
    <script>
  
  window.onload = function () {
    if(sessionStorage.jwt != '123456'){
              window.location.href = 'admin_login.html';
            }
        Vue.prototype.$http = axios;
        new Vue({
          el: '#workArea',
          data: {
            file: '',
            data2:null,
            id:0,
            name:'',
            url:'',
            page:'1',
            lastpage:'',
            msg:'',
            imgurl:'',
            token:''
            
          },
          mounted:function(){ //mounted　表示这个 Vue 对象加载成功了
                            this.init();    
                            },
          methods: {
            clear:function () {
                                this.url='';
                                this.name='';
                                this.file=null;
                                $("#Pic").val('');
                                $("#Pic1").val('');
                                },
                              get:function (id,name,url) {
                                this.id=id;
                                this.name=name;
                                this.url=url;
                },
                                init:function(){
                                  var uri='http://120.79.186.44:9092/manager/category/list';
                                  var url=uri+ "?page="+this.page;
                                 axios.get(url)
                          .then(response => {
                            this.data2 = response.data;
                            this.callastpage();
                            })    //获取到的indexDatas赋值给data2
                          .catch(function (error) { // 请求失败处理
                            console.log(error);
                            });
                                 },
                                 deleteBean: function(id) {
                                  var d = confirm("是否要删除");
                                  var uri='http://120.79.186.44:9092/manager/category/delete';
                                  var url=uri+ "?id="+id;
                                  if(d===true)               
                                   axios.post(url)
                                     .then(res => {
                                    (this.msg = res.data.status);   
                                          if(this.msg == 0)
                                  {
                                 alert("请先删除其所有作品");
                                  }
                                 else{
                                      alert("删除成功");
                                        this.init();
                                   }
                                    })     
                                 },
                                 update: function() { 
                                   
                        if(!checkEmpty(this.name, "比赛名称"))
                                   return;
                                  if(!checkEmpty(this.url, "比赛官网"))
                                   return;
if(null==this.file || this.file.length==0) {
  axios.post('http://120.79.186.44:9092/manager/category/update', {id:this.id,name:this.name,url:this.url})
                               .then(res => {
                                    if(res.status === 200) {
                                      $('#myModal').modal('hide');
                                        alert("修改成功");
                                        this.init();
                                        this.clear();
                                    }
                                  })  
}
else{
              event.preventDefault();
              let formData = new FormData();
              formData.append('file', this.file);
              let config = {
                headers: {
                  'Content-Type': 'multipart/form-data'
                }
              }
              this.$http.post('http://120.79.186.44:9092/manager/production/pic/upload', formData, config).then(res=>  {
                (this.imgurl = res.data.url);  
                if (res.status === 200) {
                axios.post('http://120.79.186.44:9092/manager/category/update', {id:this.id,name:this.name,url:this.url,img:this.imgurl})
                               .then(res => {
                                    if(res.status === 200) {
                                      $('#myModal').modal('hide');
                                        alert("修改成功");
                                        this.init();
                                        this.clear();
                                    }
                                  })  
                }
              });
                                 }},
                                  //分页
                                  paging:function(page){  
                                    if(page===0) {page=1; alert('已经是第一页了')}
                                    if(page===this.lastpage+1)  {page=this.lastpage; alert('已经是最后一页了')}
                                       this.page=page;
                                       this.init();
                                  },
                                  callastpage:function(){
                                    if(this.data2.total%5===0){
                              this.lastpage=this.data2.total/5;
                                         }
                                   else
                                  {
                                  this.lastpage = (this.data2.total-this.data2.total%5)/5+1;
                                    }
                                  },
            getFile(event) {
              this.file = event.target.files[0];
              console.log(this.file);
            },
            submitForm(event) {
              
                        if(!checkEmpty(this.name, "比赛名称"))
                                   return;
                                  if(!checkEmpty(this.url, "比赛官网"))
                                   return;
if(!checkEmpty(this.file, "比赛图片"))
                        return;
              event.preventDefault();
              let formData = new FormData();
              formData.append('file', this.file);
              let config = {
                headers: {
                  'Content-Type': 'multipart/form-data'
                }
              }
              this.$http.post('http://120.79.186.44:9092/manager/production/pic/upload', formData, config).then(res=>  {
                (this.imgurl = res.data.url);  
                if (res.status === 200) {
                axios.post('http://120.79.186.44:9092/manager/category/insert', {name:this.name,url:this.url,img:this.imgurl})
                               .then(res => {
                                    (this.token = res.data);
                                    if(this.token.status != 0) {
                                      $('#myModal1').modal('hide');
                                        alert("添加成功");
                                        this.init();
                                        this.clear();
                                    }
                                  
                                    if(this.token.status == 0){
                                      alert("该比赛已经存在");
                                    }


                                  })  
                
                }
              });
       
            }
          }
        });
        Vue.filter('formatDateFilter', function (value, formatString) {
            if(null==value)
                return "";
            formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
            return moment(value).format(formatString);
        });
      
      }
           </script>      
    <div class="page"   id="workArea">
        <!--头部-->
        <header class="header">
            <nav class="navbar">
              <div class="container-fluid">
                <div class="navbar-holder d-flex align-items-center justify-content-between">
                  <div class="navbar-header">
                    <a href="#" class="navbar-brand d-none d-sm-inline-block">
                      <div class="brand-text d-none d-lg-inline-block"><span>USC-SE </span><strong>ADMIN</strong></div>
                    </a>
                  </div>

                </div>
              </div>
            </nav>
          </header>
       
        <div class="page-content d-flex align-items-stretch"> 
          
          
          <!-- 左部 -->
          <nav class="side-navbar">
            <!-- 头像名字部分-->
            <div class="sidebar-header d-flex align-items-center">
                <div class="avatar"><img src="img/admin.jpg" alt="..." class="img-fluid rounded-circle"></div>
                <div class="title">
                  <h1 class="h4">USC</h1>
                  <p>Welcome</p>
                </div>
            </div>
  
            <!-- 左边菜单-->
            <span class="heading">管理</span>
            <ul class="list-unstyled">
              <li class="active"><a href="admin_category.html">比赛管理 </a></li>
              <li><a href="admin_user.html">用户管理</a></li>
              <li ><a href="admin_tags.html">标签管理 </a></li>    
              <li ><a href="admin_news.html">近期新闻管理 </a></li>
              <li ><a href="admin_newsswiper.html">新闻轮播图管理 </a></li> 
              <li ><a href="admin_competitionswiper.html">比赛轮播图管理 </a></li> 
            </ul>
          </nav>

          
          <!-- 右部表格部分 -->
          <div class="content-inner"> 
            <!-- 管理标题-->
            <header class="page-header">
              <div class="container-fluid">
                <h2 class="no-margin-bottom">
                  比赛管理  
            </h2>
              </div>
            </header>
            
            <!-- 表格 -->
            <section class="tables">   
              <div class="container-fluid">
                <div class="row">
                  <div class="col-lg-12">
                    <div class="card">
                      <div class="card-body">
                        <div class="table-responsive">  
                          <div>
                            <table class="table table-striped">
                                <thead>
                                  <tr>
                                    <th>比赛名称</th>
                                    <th>url</th>
                                    <th>图片</th>
                                    <th>创建时间</th>
                                    <th>最新更新时间</th>
                                    <th>作品管理</th>
                                    <th>编辑比赛</th>
                                    <th>删除比赛</th>
                                  </tr>
                                </thead>
                                <tbody>
                                  <tr v-for="bean in data2.rows">                          
                                    <td>{{bean.name}}</td>
                                    <td><a :href="bean.url">{{bean.url}}</a></td>
                                    <td><img v-bind:src="bean.img"  width="100"></td>
                                    <td>{{bean.created | formatDateFilter}}</td>
                                    <td>{{bean.updated | formatDateFilter}}</td>
                                    <td><a :href="'admin_production.html?cid=' + bean.id+'&cname='+bean.name "><img src="img/manage.jpg" width="30" ></a></td>
                                    <td><a herf="#" @click="get(bean.id,bean.name,bean.url)"><span data-toggle="modal" data-target="#myModal"><img src="img/edit.jpg" width="30" ></span></a></td>
                                    <td><a href="#nowhere"  @click="deleteBean(bean.id)"><img src="img/delete.jpg" width="30" ></a></td>
                                  </tr>
                                </tbody>
                              </table>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
            </section>  
            <div align="center">
                <span>
                    <ul class="pagination">
                        <li class="page-item"><a class="page-link" href="#" @click="paging(1)">首页</a></li>
                        <li class="page-item"><a class="page-link" href="#" @click="paging(--page)">上一页</a></li>           
                        <li class="page-item"><a class="page-link" href="#" @click="paging(page)">页数：{{page}}</a></li>
                        <li class="page-item"><a class="page-link" href="#" @click="paging(++page)">下一页</a></li>
                        <li class="page-item"><a class="page-link" href="#" @click="paging(lastpage)">尾页</a></li>
                    </ul>
            
                </span>
               </div> 


               <div class="modal fade" id="myModal"  data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">修改比赛信息</h4>
                        </div>
                        <div class="modal-body">
                            <div class="panel-body">
                                <table class="table table-hover" align="center">
                                    <tr>
                                        <td>比赛名称</td>
                                        <td><input   id="name" type="text" autocomplete="off" v-model="name" class="form-control" placeholder="请在这里输入比赛名称"></td>
                                    </tr>
                                    <tr>
                                        <td>比赛官网</td>
                                        <td><input   id="url"  type="text" autocomplete="off" v-model="url" class="form-control" placeholder="请在这里输入比赛官网"></td>
                                    </tr>
                                    <tr>
                                            <td>请选择一张大赛图片：</td>
                                            <td><input    type="file" @change="getFile($event)" id="Pic1"></td>
            
                                        </tr>
                                </table>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button data-dismiss="modal" class="btn btn-default" type="button" @click="clear">关闭</button>
                            <button  class="btn btn-default" type="button" @click="update">提交</button>
  
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div>
  
             <!-- 添加模态窗口 -->
             <div align="center">
              <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal1" >
                   添加数据
              </button>
          </div>
          <div class="modal fade" id="myModal1"  data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">添加比赛信息</h4>
                    </div>
                    <div class="modal-body">
                        <div class="panel-body">
                            <table class="table table-hover" align="center" >
                              <tr>
                                <td>比赛名称</td>
                                <td><input   id="name" type="text" autocomplete="off" v-model="name" class="form-control" placeholder="请在这里输入比赛名称"></td>
                            </tr>
                            <tr>
                                <td>比赛官网</td>
                                <td><input   id="url"  type="text" autocomplete="off" v-model="url" class="form-control" placeholder="请在这里输入比赛官网"></td>
                            </tr>
                            <tr>
                                <td>请选择一张大赛图片：</td>
                                <td><input    type="file" @change="getFile($event)" id="Pic"></td>

                            </tr>
                            
                            </table>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button data-dismiss="modal" class="btn btn-default" type="button" >关闭</button>
                        <button  class="btn btn-default" type="button" @click="submitForm($event)">提交</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>

  
             <!-- 页脚-->
             <footer class="main-footer">
              <div class="container-fluid">
                <div class="row">
                  <div class="col-sm-6">
                    <p>USC-SE</p>
                  </div>
                  <div class="col-sm-6 text-right">
                    <p>Powered by <a href="https://bootstrapious.com/p/admin-template" class="external">Bootstrapious</a></p>
                </div>
              </div>
            </footer>
  
          </div>
        </div>
      </div>
      
</body>
</html>